package com.bodacn.keypoint.adapter;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.bodacn.keypoint.R;
import com.bodacn.keypoint.bean.ClothPieceCheckRight;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class ClothSizeSearchRightLvAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private Context mContext;
    private List<ClothPieceCheckRight> list;
    private DecimalFormat fnum;

    public ClothSizeSearchRightLvAdapter(List<ClothPieceCheckRight> list, Context context) {
        this.inflater = LayoutInflater.from(context);
        this.mContext = context;
        this.list = list;
        fnum = new DecimalFormat("##0.0");
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHodler;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_cloth_size_check_right, parent, false);
            viewHodler = new ViewHolder(convertView);
            convertView.setTag(viewHodler);
        } else {
            viewHodler = (ViewHolder) convertView.getTag();
        }
        if (list.size() > 0) {
            ClothPieceCheckRight clothPieceCheckRight = list.get(position);
            viewHodler.tvPartName.setText(clothPieceCheckRight.getPartName());
            viewHodler.tvNormalSize.setText(clothPieceCheckRight.getStandSize());
            viewHodler.edRealSize.setText(clothPieceCheckRight.getRealSize());

            viewHodler.tvDown.setText(clothPieceCheckRight.getUp());
            viewHodler.tvUp.setText(clothPieceCheckRight.getUp());
            if (!TextUtils.isEmpty(clothPieceCheckRight.getRealSize())&&!TextUtils.isEmpty(clothPieceCheckRight.getStandSize())
                    &&!TextUtils.isEmpty(clothPieceCheckRight.getUp())&&!TextUtils.isEmpty(clothPieceCheckRight.getDown())){
                double num=Double.parseDouble(clothPieceCheckRight.getRealSize())-Double.parseDouble(clothPieceCheckRight.getStandSize());
                if (num>=-Double.parseDouble(clothPieceCheckRight.getDown())&&num<=Double.parseDouble(clothPieceCheckRight.getUp())){
                    viewHodler.edRealSize.setBackground(mContext.getResources().getDrawable(R.drawable.btn_white_green_line));
                }else {
                    viewHodler.edRealSize.setBackground(mContext.getResources().getDrawable(R.drawable.btn_white_red_line));
                }
            }

        }

        return convertView;
    }




    class ViewHolder {
        @BindView(R.id.tv_PartName)
        TextView tvPartName;
        @BindView(R.id.tv_NormalSize)
        TextView tvNormalSize;
        @BindView(R.id.tv_up)
        TextView tvUp;
        @BindView(R.id.tv_down)
        TextView tvDown;
        @BindView(R.id.ed_realSize)
        TextView edRealSize;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
